package com.sls.game.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.http.HttpClient;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.PropertiesUtil;
import com.sls.core.util.ServerDateAndTime;
import com.sls.core.util.UtilMD5Encryption;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.EBaiMiDAO;
import com.sls.game.dao.EbaimiCheckFileDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.ebaimi.EbaimiCheckFileInfo;
import com.sls.game.pojo.ebaimi.EbaimiProduct;
import com.sls.game.pojo.ebaimi.EbaimiRequestInfo;
import com.sls.game.pojo.ebaimi.EbaimiResInfo;
import com.sls.game.pojo.ebaimi.ZhifubaoSendMsgInfo;
import com.sls.game.service.EBaiMiService;
import com.sls.game.supply.deal.EBaiMiTradeDeal;

@Service("eBaiMiService")
public class EBaiMiServiceImpl implements EBaiMiService {

	private static Logger log = Logger.getLogger(EBaiMiServiceImpl.class);
	private static String fileName = "ebaimi.properties";
	private Gson gson;
	private String md5key;
	private String loginId;
	@Resource(name = "eBaiMiDAO")
	private EBaiMiDAO eBaiMiDAO;

	@Resource(name = "ebaimiCheckFileDAO")
	private EbaimiCheckFileDAO ebaimiCheckFileDAO;

	public EBaiMiServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void saveSync(Head head) {
		// 商品列表下载地址
		String goodsListAddress = PropertiesUtil.getValue(
				"ebaimi.interface.goods.list.address", fileName);
		String parameter = this.assembleParameterForDownGoodsList();
		HttpClient httpClient = new HttpClient();
		EbaimiResInfo resInfo = null;
		try {
			// 对返回的gson格式字符串数据进行解析
			Map<String, String> map = new HashMap<String, String>();
			map.put("parameter", parameter);
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米的同步产品请求参数："
					+ parameter);
			String gsonStr = httpClient.doPost(goodsListAddress, map);
			log.info("请求ID：" + head.getRequestId() + "--易百米返回给手拉手的请求结果："
					+ gsonStr);
			if (StringUtils.isNotEmpty(gsonStr)) {
				resInfo = gson.fromJson(gsonStr, EbaimiResInfo.class);
				if (resInfo != null) {
					List<EbaimiProduct> list = resInfo.getGoodslist();
					this.saveGoodsList(list);
				}
			}
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米同步产品请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
	}

	@Override
	public EbaimiResInfo balanceQuery(Head head) {
		String parameter = this.assembleParameterForBalanceQuery();
		HttpClient httpClient = new HttpClient();
		String balanceAddress = PropertiesUtil.getValue(
				"ebaimi.interface.query.balance.address", fileName);
		EbaimiResInfo resInfo = null;
		try {
			// 对返回的gson格式字符串数据进行解析
			Map<String, String> map = new HashMap<String, String>();
			map.put("parameter", parameter);
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米余额查询请求参数："
					+ parameter);
			String gsonStr = httpClient.doPost(balanceAddress, map);
			log.info("请求ID：" + head.getRequestId() + "--易百米余额查询返回给手拉手的请求结果："
					+ gsonStr);
			if (StringUtils.isNotEmpty(gsonStr)) {
				resInfo = gson.fromJson(gsonStr, EbaimiResInfo.class);
			}
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米余额查询请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
		return resInfo;
	}

	@Override
	public EbaimiResInfo orderQuery(String body, Head head) {
		EbaimiRequestInfo rInfo = gson.fromJson(body, EbaimiRequestInfo.class);
		if (StringUtils.isEmpty(rInfo.getJno_cli())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SLS_ORDERNO_NULL.getCode(),
					ExceptionCodeDes.SLS_ORDERNO_NULL.getDescription());
		}
		String parameter = this.assembleParameterForOrderQuery(rInfo
				.getJno_cli());
		HttpClient httpClient = new HttpClient();
		String orderAddress = PropertiesUtil.getValue(
				"ebaimi.interface.query.order.address", fileName);
		EbaimiResInfo resInfo = null;
		try {
			// 对返回的gson格式字符串数据进行解析
			Map<String, String> map = new HashMap<String, String>();
			map.put("parameter", parameter);
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米订单查询请求参数："
					+ parameter);
			String gsonStr = httpClient.doPost(orderAddress, map);
			log.info("请求ID：" + head.getRequestId() + "--易百米订单查询返回给手拉手的请求结果："
					+ gsonStr);
			if (StringUtils.isNotEmpty(gsonStr)) {
				resInfo = gson.fromJson(gsonStr, EbaimiResInfo.class);
			}
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米订单查询请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
		return resInfo;
	}

	@Override
	public String checkAccount(Head head) {
		String parameter = this.assembleParameterForCheckAccount();
		HttpClient httpClient = new HttpClient();
		String checkAccountAddress = PropertiesUtil.getValue(
				"ebaimi.interface.check.account.address", fileName);
		try {
			// 对返回的gson格式字符串数据进行解析
			Map<String, String> map = new HashMap<String, String>();
			map.put("parameter", parameter);
			log.info("请求ID：" + head.getRequestId() + "--发送到易百米对账文件请求参数："
					+ parameter);
			String gsonStr = httpClient.doPost(checkAccountAddress, map);
			log.info("请求ID：" + head.getRequestId() + "--易百米对账文件返回给手拉手的请求结果："
					+ gsonStr);
			if (StringUtils.isNotEmpty(gsonStr)) {
				return gsonStr;
			}
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米订单查询请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
		return null;
	}

	private String assembleParameterForCheckAccount() {
		EbaimiRequestInfo reqInfo = new EbaimiRequestInfo();
		md5key = PropertiesUtil.getValue("ebaimi.md5.key", fileName);
		loginId = PropertiesUtil.getValue("ebaimi.interface.oid.loginid",
				fileName);
		reqInfo.setOid_loginid(loginId);
		String sign = UtilMD5Encryption.createMD5Encryption(loginId + md5key)
				.toUpperCase();
		reqInfo.setSign(sign);
		String parameter = gson.toJson(reqInfo);
		return parameter;
	}

	private String assembleParameterForOrderQuery(String orderId) {
		EbaimiRequestInfo reqInfo = new EbaimiRequestInfo();
		md5key = PropertiesUtil.getValue("ebaimi.md5.key", fileName);
		loginId = PropertiesUtil.getValue("ebaimi.interface.oid.loginid",
				fileName);
		reqInfo.setOid_loginid(loginId);
		reqInfo.setJno_cli(orderId);
		String sign = UtilMD5Encryption.createMD5Encryption(
				loginId + orderId + md5key).toUpperCase();
		reqInfo.setSign(sign);
		String parameter = gson.toJson(reqInfo);
		return parameter;
	}

	private String assembleParameterForBalanceQuery() {
		EbaimiRequestInfo reqInfo = new EbaimiRequestInfo();
		md5key = PropertiesUtil.getValue("ebaimi.md5.key", fileName);
		loginId = PropertiesUtil.getValue("ebaimi.interface.oid.loginid",
				fileName);
		reqInfo.setOid_loginid(loginId);
		String sign = UtilMD5Encryption.createMD5Encryption(loginId + md5key)
				.toUpperCase();
		reqInfo.setSign(sign);
		String parameter = gson.toJson(reqInfo);
		return parameter;
	}

	private String assembleParameterForDownGoodsList() {
		EbaimiRequestInfo reqInfo = new EbaimiRequestInfo();
		md5key = PropertiesUtil.getValue("ebaimi.md5.key", fileName);
		loginId = PropertiesUtil.getValue("ebaimi.interface.oid.loginid",
				fileName);
		reqInfo.setOid_loginid(loginId);
		String sign = UtilMD5Encryption.createMD5Encryption(loginId + md5key)
				.toUpperCase();
		reqInfo.setSign(sign);
		String parameter = gson.toJson(reqInfo);
		return parameter;
	}

	private void saveGoodsList(final List<EbaimiProduct> list) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				if (list != null && !list.isEmpty()) {
					for (EbaimiProduct eInfo : list) {
						// 查询是否有重复的
						EbaimiProduct ebaimi = eBaiMiDAO.queryByGoodsId(eInfo
								.getOid_goodsitem());
						try {
							if (ebaimi == null) {
								eBaiMiDAO.insert(eInfo);
							} else {
								eBaiMiDAO.update(eInfo);
							}
						} catch (Exception e) {
							log.error("商品编号：" + eInfo.getOid_goodsitem()
									+ "同步异常", e);
							continue;
						}
					}
				}
			}
		}).start();
	}

	@Override
	public Pagination<EbaimiProduct> queryPage(String body, Head head) {
		EbaimiProduct ep = gson.fromJson(body, EbaimiProduct.class);
		this.validate(ep);
		try {
			Pagination<EbaimiProduct> page = this.eBaiMiDAO
					.queryPaginationList(ep);
			return page;
		} catch (Exception e) {
			log.error(e.toString(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

	@Override
	public void update(String body, Head head) {
		EbaimiProduct ep = gson.fromJson(body, EbaimiProduct.class);
		this.validate(ep);
		try {
			ep.setIsAudit("0");
			this.eBaiMiDAO.update(ep);
		} catch (Exception e) {
			log.error(e.toString(), e);
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}

	}

	private void validate(EbaimiProduct ep) {
		if (ep == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	@Override
	public EbaimiProduct queryById(String body, Head head) {
		String id = gson.fromJson(body, String.class);
		if (StringUtils.isEmpty(id)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		}
		EbaimiProduct ep = null;
		try {
			ep = this.eBaiMiDAO.queryById(id);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return ep;
	}

	/**
	 * 支付宝充值--订单查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-10
	 */
	@Override
	public String queryOrderStatusOfZhifubao(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		ZhifubaoSendMsgInfo info = gson.fromJson(body,
				ZhifubaoSendMsgInfo.class);
		boolean flag = checkDataOfOrderStatus(info);
		if (flag) {
			// 合作方的登陆ID(目前为11位)
			String oid_loginid = PropertiesUtil.getValue(
					"ebaimi.zhifubao.oid_loginid", "ebaimi.properties");
			if (oid_loginid != null && !("".equals(oid_loginid))) {
				oid_loginid = oid_loginid.trim();
			}
			// 自己平台订单号
			String jno_cli = info.getJno_cli();
			String jsonStr = sendQueryOrderStatusMsg(oid_loginid, jno_cli, head);
			return jsonStr;
		}

		return null;
	}

	/**
	 * 发送订单状态查询
	 * 
	 * @param oid_loginid
	 * @param jno_cli
	 * @param head
	 * @return
	 */
	private String sendQueryOrderStatusMsg(String oid_loginid, String jno_cli,
			Head head) {
		// 密钥
		String securitykey = PropertiesUtil.getValue("ebaimi.zhifubao.key",
				"ebaimi.properties");
		if (securitykey != null && !("".equals(securitykey))) {
			securitykey = securitykey.trim();
		}

		StringBuilder strBui = new StringBuilder();
		strBui.append(oid_loginid);
		strBui.append(jno_cli);
		strBui.append(securitykey);

		String data = strBui.toString().trim();
		String sign = UtilMD5Encryption.createMD5Encryption(data);
		if (sign != null && !("".equals(sign))) {
			sign = sign.toUpperCase();
		}

		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append("{");
		strBuilder.append("\"oid_loginid\":");
		strBuilder.append("\"" + oid_loginid + "\"");
		strBuilder.append(",");
		strBuilder.append("\"jno_cli\":");
		strBuilder.append("\"" + jno_cli + "\"");
		strBuilder.append(",");
		strBuilder.append("\"sign\":");
		strBuilder.append("\"" + sign + "\"");
		strBuilder.append("}");
		String sendMsg = strBuilder.toString().trim();

		HttpClient httpClient = new HttpClient();
		String supplyEncode = "UTF-8";
		try {
			String address = PropertiesUtil.getValue(
					"ebaimi.zhifubao.orderStatus.address", "ebaimi.properties");
			if (address != null && !("".equals(address))) {
				address = address.trim();
			}
			String jsonStr = httpClient.doPost(address, sendMsg, supplyEncode);
			return jsonStr;
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米订单状态查询请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
	}

	/**
	 * 
	 * @param info
	 * @return
	 */
	private boolean checkDataOfOrderStatus(ZhifubaoSendMsgInfo info) {
		boolean flag = false;
		// 自己平台订单号
		String jno_cli = info.getJno_cli();
		if ("".equals(jno_cli) || jno_cli == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.SLS_ORDERID_NULL.getCode(),
					ExceptionCodeDes.SLS_ORDERID_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 支付宝充值--余额查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-10
	 */
	@Override
	public String queryBalanceOfZhifubao(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		String jsonStr = sendBalanceMsg(head);

		return jsonStr;
	}

	/**
	 * 
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-10
	 */
	private String sendBalanceMsg(Head head) {
		// 合作方的登陆ID(目前为11位)
		String oid_loginid = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_loginid", "ebaimi.properties");
		// 密钥
		String securitykey = PropertiesUtil.getValue("ebaimi.zhifubao.key",
				"ebaimi.properties");
		if (oid_loginid != null && !("".equals(oid_loginid))) {
			oid_loginid = oid_loginid.trim();
		}
		if (securitykey != null && !("".equals(securitykey))) {
			securitykey = securitykey.trim();
		}

		String data = oid_loginid + securitykey;
		String sign = UtilMD5Encryption.createMD5Encryption(data);
		if (sign != null && !("".equals(sign))) {
			sign = sign.toUpperCase();
		}
		ZhifubaoSendMsgInfo sendInfo = new ZhifubaoSendMsgInfo();
		sendInfo.setOid_loginid(oid_loginid);
		sendInfo.setSign(sign);
		String sendMsg = gson.toJson(sendInfo);

		HttpClient httpClient = new HttpClient();
		String supplyEncode = "UTF-8";
		try {
			String address = PropertiesUtil.getValue(
					"ebaimi.zhifubao.balance.address", "ebaimi.properties");
			if (address != null && !("".equals(address))) {
				address = address.trim();
			}

			String jsonStr = httpClient.doPost(address, sendMsg, supplyEncode);
			return jsonStr;
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米余额查询请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
	}

	/**
	 * 合作商单日对账接口
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-10
	 */
	@Override
	public String saveCheckAccountOfZhifubao(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		String fileStr = sendCheckAccountMsg(head);

		// 把获取的对账文件插入到数据库中
		List<EbaimiCheckFileInfo> list = EBaiMiTradeDeal.parseBill(fileStr);
		insertBill(list);

		return fileStr;
	}

	/**
	 * 把账单信息插入数据库
	 * 
	 * @param list
	 */
	private void insertBill(List<EbaimiCheckFileInfo> list) {
		for (EbaimiCheckFileInfo info : list) {
			try {
				String oid_goodsorder = info.getOid_goodsorder();
			String primaryKey = ebaimiCheckFileDAO.queryPrimaryKey(oid_goodsorder);
				if (primaryKey == null || "".equals(primaryKey)) { //插入
					ebaimiCheckFileDAO.insertInfo(info);
				}else { //更新
					ebaimiCheckFileDAO.updateInfo(info);
				}
			} catch (Exception e) {
				log.error(e);
			}

		}
	}

	/**
	 * 
	 * @param head
	 * @return
	 */
	private String sendCheckAccountMsg(Head head) {
		// 合作方的登陆ID(目前为11位)
		String oid_loginid = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_loginid", "ebaimi.properties");
		// 密钥
		String securitykey = PropertiesUtil.getValue("ebaimi.zhifubao.key",
				"ebaimi.properties");
		if (oid_loginid != null && !("".equals(oid_loginid))) {
			oid_loginid = oid_loginid.trim();
		}
		if (securitykey != null && !("".equals(securitykey))) {
			securitykey = securitykey.trim();
		}
		String data = oid_loginid + securitykey;
		String sign = UtilMD5Encryption.createMD5Encryption(data);
		if (sign != null && !("".equals(sign))) {
			sign = sign.toUpperCase();
		}
		ZhifubaoSendMsgInfo sendInfo = new ZhifubaoSendMsgInfo();
		sendInfo.setOid_loginid(oid_loginid);
		sendInfo.setSign(sign);
		String sendMsg = gson.toJson(sendInfo);

		HttpClient httpClient = new HttpClient();
		String supplyEncode = "GBK";
		try {
			String address = PropertiesUtil
					.getValue("ebaimi.zhifubao.checkAccount.address",
							"ebaimi.properties");
			if (address != null && !("".equals(address))) {
				address = address.trim();
			}

			String fileStr = httpClient.doPost(address, sendMsg, supplyEncode);
			return fileStr;
		} catch (Exception e) {
			log.error("请求ID：" + head.getRequestId() + "--易百米获取单日对账文件请求异常！", e);
			throw new ServerDisponseException(
					ExceptionCodeDes.HTTP_URL_FAILED.getCode(),
					ExceptionCodeDes.HTTP_URL_FAILED.getDescription());
		}
	}

	/**
	 * 支付宝商品--记录新增
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	@Override
	public String saveGoodsInfoOfZhifubao(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		EbaimiProduct product = gson.fromJson(body, EbaimiProduct.class);
		boolean flag = checkDataOfGoodsAdd(product);
		if (flag) {
			// 易百米的商品编号
			String oid_goodsitem = product.getOid_goodsitem();
			EbaimiProduct proInfo = eBaiMiDAO.queryByGoodsId(oid_goodsitem);
			if (proInfo != null) { // 更新
				try {
					String id = proInfo.getId();
					String modify_time = ServerDateAndTime
							.getNowDateTimeOfFormat();
					product.setId(id);
					product.setModify_time(modify_time);
					product.setIsAudit("0");
					eBaiMiDAO.update(product);
				} catch (Exception e) {
					log.error(e.toString(), e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			} else { // 新增
				try {
					String id = UUID.randomUUID().toString();
					String insert_time = ServerDateAndTime
							.getNowDateTimeOfFormat();
					String oid_biz = PropertiesUtil.getValue(
							"ebaimi.zhifubao.oid_biz", "ebaimi.properties");
					if (oid_biz != null && !("".equals(oid_biz))) {
						oid_biz = oid_biz.trim();
					}

					product.setId(id);
					product.setInsert_time(insert_time);
					product.setIsAudit("0");
					product.setOid_biz(oid_biz);
					eBaiMiDAO.insert(product);
				} catch (Exception e) {
					log.error(e.toString(), e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}
			}
		}
		return null;
	}

	/**
	 * 商品新增校验
	 * 
	 * @param product
	 * @return
	 */
	private boolean checkDataOfGoodsAdd(EbaimiProduct product) {
		boolean flag = false;
		// 易百米的商品编号
		String oid_goodsitem = product.getOid_goodsitem();
		// 商品名称
		String name_goods = product.getName_goods();
		// 商品面值(以元为单位，精度为分)
		String price = product.getPrice();
		// 商品进价(以元为单位，精度为分)
		String sup_price = product.getSup_price();

		if (oid_goodsitem == null || ("".equals(oid_goodsitem))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else if (name_goods == null || ("".equals(name_goods))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_NAME_NULL.getCode(),
					ExceptionCodeDes.GOODS_NAME_NULL.getDescription());
		} else if (price == null || ("".equals(price))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getCode(),
					ExceptionCodeDes.GOODS_PARVALUE_NULL.getDescription());
		} else if (sup_price == null || ("".equals(sup_price))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_IN_PRICE_NULL.getCode(),
					ExceptionCodeDes.GOODS_IN_PRICE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 支付宝商品--记录删除
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String deleteGoodsInfoOfZhifubao(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		EbaimiProduct product = gson.fromJson(body, EbaimiProduct.class);
		boolean flag = checkDataOfDelete(product);
		if (flag) {
			eBaiMiDAO.deleteGoods(product);
		}

		return null;
	}

	/**
	 * 
	 * @param product
	 * @return
	 */
	private boolean checkDataOfDelete(EbaimiProduct product) {
		boolean flag = false;
		String id = product.getId();
		if (id == null || ("".equals(id))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 合作方的易百米商户ID--查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	@Override
	public String queryOid_loginid(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		String oid_loginid = PropertiesUtil.getValue(
				"ebaimi.zhifubao.oid_loginid", "ebaimi.properties");
		if (oid_loginid != null && !("".equals(oid_loginid))) {
			oid_loginid = oid_loginid.trim();
		}
		EbaimiProduct ebaimiProduct = new EbaimiProduct();
		ebaimiProduct.setOid_loginid(oid_loginid);
		String resultBody = gson.toJson(ebaimiProduct);

		return resultBody;
	}

	/**
	 * 对账表--分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 */
	@Override
	public String queryBillTableByPage(String body, Head head) {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}

		EbaimiCheckFileInfo billInfo = gson.fromJson(body,
				EbaimiCheckFileInfo.class);
		boolean flag = checkDataOfBill(billInfo);
		if (flag) {

			String billDate = billInfo.getBillDate();
			String billDateStart = billDate.trim() + " " + "00:00:00";
			String billDateEnd = billDate.trim() + " " + "23:59:59";
			billInfo.setBillDateStart(billDateStart.trim());
			billInfo.setBillDateEnd(billDateEnd.trim());
			Pagination<EbaimiCheckFileInfo> page = ebaimiCheckFileDAO
					.queryPaginationList(billInfo);
			String resultBody = gson.toJson(page);
			return resultBody;
		}

		return null;
	}

	/**
	 * 
	 * @param product
	 * @return
	 */
	private boolean checkDataOfBill(EbaimiCheckFileInfo billInfo) {
		boolean flag = false;
		String pageSize = billInfo.getPageSize() + "";
		String currentPage = billInfo.getCurrentPage() + "";
		String billDate = billInfo.getBillDate();

		if (billDate == null || ("".equals(billDate))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DATE_NULL.getCode(),
					ExceptionCodeDes.DATE_NULL.getDescription());
		} else if (pageSize == null || ("".equals(pageSize))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else if (currentPage == null || ("".equals(currentPage))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	// public static void main(String[] args) throws Exception{
	// String address =
	// "http://test.ebaimi.com/LIANPAY_UNITSUPGW/game/getBalance.action";
	// // String address = "http://192.168.1.183:8000/api_game/notifyUrl";
	//
	// String oid_loginid = "13500001112";
	// String sign = "C691BA75F416612E4DA171AB89B7A2D4";
	// ZhifubaoSendMsgInfo sendInfo = new ZhifubaoSendMsgInfo();
	// sendInfo.setOid_loginid(oid_loginid);
	// sendInfo.setSign(sign);
	//
	// Gson mygson = new Gson();
	// String sendMsg = mygson.toJson(sendInfo);
	// HttpClient httpClient = new HttpClient();
	// String supplyEncode = "UTF-8";
	// String str = httpClient.doPost(address, sendMsg, supplyEncode);
	// System.out.println(str);
	//
	// }

}
